angular.module('app.order')
    .controller('orderCtrl', ['$scope', '$filter', '$location', '$modal', 'toaster', 'orderService',
        function ($scope, $filter, $location, $modal, toaster, orderService) {
            $scope.orders = [];
            $scope.search;
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };

            $scope.startTime;
            $scope.endTime;

            $scope.status = {};
            $scope.status.selected= {"id":1,"name":"全部"};
            $scope.statuses=[{"id":1,"name":"全部"},{"id":2,"name":"未确认"},{"id":3,"name":"已确认"}];

            $scope.options = {
                allowInvalid: false,
                timezone: 'CCT'
            };

            $scope.searchOrder = function () {
                var query = {};
                query.search = $scope.search;
                query.pageNo = $scope.pager.pageNo;
                query.pageSize = $scope.pager.pageSize;
                query.startTime = $filter('date')($scope.startTime, 'yyyy-MM-dd HH:mm:ss');
                query.endTime = $filter('date')($scope.endTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.status.selected.id == 2) {
                    query.status = 0;
                } else if ($scope.status.selected.id == 3) {
                    query.status =1;
                }

                orderService.orderList(query).then(function (res) {
                    $scope.orders = res.data.data;
                    $scope.pager.totalItems = res.data.totalItems;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };
            $scope.searchOrder();

            $scope.confirmOrder = function (e) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "订单已经收款?";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    orderService.confirmOrder(e.id).then(function (res) {
                        toaster.pop("success", "成功", "订单确认成功");
                        $scope.searchOrder();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "订单确认失败")
                    });
                });
            };

            $scope.removeOrder = function (e) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除该订单?";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    orderService.remove(e.id).then(function (res) {
                        toaster.pop("success", "成功", "订单删除成功");
                        $scope.searchOrder();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "订单删除失败")
                    });
                });
            };
        }]);
